# 爬取斗鱼英雄联盟主播人气数据  需要异步io优化
from urllib import request
import re


class DouYuTvSpider(object):
    root_page = 'data-total="([\s\S]*?)"'
    root_parent = '<span class="item-info clearfix">([\s\S]*?) </span>'
    name_parent = '<strong>([\s\S]*?)</strong>'
    num_parent = '<b>([\s\S]*?)</b>'

    # 获取内容
    def __douyu_html_content(self, i):
        url = "https://v.douyu.com/directory/catelist/5"
        user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " \
                     "AppleWebKit/537.36 (KHTML, like Gecko) " \
                     "Chrome/70.0.3538.77 Safari/537.36"
        if i > 0:
            url = url + "?page=" + str(i)
        req = request.Request(url)
        req.add_header("User-Agent", user_agent)
        with request.urlopen(req) as r:
            data = r.read()
            htmls = data.decode("utf-8")
            return htmls

    # 解析内容
    def __analysis(self, htmls, analysis_info_list):
        hmt_list = re.findall(DouYuTvSpider.root_parent, htmls)
        for html in hmt_list:
            name = re.findall(DouYuTvSpider.name_parent, html)
            num = re.findall(DouYuTvSpider.num_parent, html)
            anchor_info = {"name": "".join(name), "num": "".join(num)}
            analysis_info_list.append(anchor_info)

    # 排序
    def sort(self, analysis_info_list):
        analysis_info_list = sorted(analysis_info_list, key=self.__sort_comparators, reverse=True)
        return analysis_info_list

    # 排序验证方法
    def __sort_comparators(self, analysis_info):
        num_str = analysis_info["num"]
        r = re.findall("\d*", num_str)
        num = float(r[0])
        if "万" in num_str:
            num = num * 10000
        return num

    # 显示内容
    def show(self, analysis_info_list):
        for code in range(0, len(analysis_info_list)):
            print("编号", str(code + 1) + " ",
                  "主播名称: ", analysis_info_list[code]["name"], " ",
                  "主播人气:", analysis_info_list[code]["num"])

    def page(self):
        htmls = self.__douyu_html_content(0)
        page = re.findall(DouYuTvSpider.root_page, htmls)
        return page

    def go(self, page_list, analysis_info_list):
        for i in page_list:
            htmls = self.__douyu_html_content(i)
            self.__analysis(htmls, analysis_info_list)
